<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//首页控制器
class ListController extends Controller{
	private $model;
	private $cate;
	private $goodsattr;
	//构造函数
	public function __init(){
		$this->model=new \Common\Model\Goods;
		$this->cate=new \Common\Model\Cate;
		$this->goodsattr=new \Common\Model\Goodsattr;
	}
	
	
	public function index(){
		//获得分类的cid
		$cid=Q('get.cid',0,'inval');
		///获得对应的cid数据
		$cate=$this->cate->where("cid={$cid}")->get();
		//分配到模板
		View::with('cate',$cate);
		
		//获取元素的子集；
		//获得所有的分类数据
		$cateData=$this->cate->get();
		//获得所有的子集的cid
		$cids=$this->cate->getSon($cateData,$cid);
		//把父级的cid也压入到自己的数组里面
		$cids[]=$cid;
		//获得商品数据；
//		$goodsData=$this->model->where("classify_cid in (".implode(',',$cids). ")")->get();
		//p($goodsData);
		
		
		//1根据cid获得对应的商品gid
		$gids=$this->cidGetGids(Q('get.cid',0,'intval'));
		if(!$gids) $this->error('暂时没有商品');
//		p($gids);
		//2根据gid找到商品的商品属性
		$goodsAttr=$this->gidGetAttr($gids);
//		p($goodsAttr);
		View::with('goodsAttr',$goodsAttr);
		//3，处理筛选参数param 0_0_
		$num=count($goodsAttr);
		$param=isset($_GET['param'])?explode('_', $_GET['param']):array_fill(0,$num,0);
		View::with('param',$param);
		//4通过param参数筛选商品gid
		$resultGids=$this->filter($param,$gids);
		if(!$resultGids) $this->error('meiyo');
		//获得商品数据
		$goodsData=$this->model->where("gid in (".implode(',',$resultGids).")")->get();
		
//		p($goodsData);
		//分配到模版
		View::with('goodsData',$goodsData);

		//载入模板
	    View::make();
		
	}
	private function filter($param,$cidGids){
		$gids=array();
		foreach ($param as $gtid){
			if($gtid){
				//1找到attvalue 
				$attvalue=$this->goodsattr->where("gaid={$gtid}")->pluck('gavalue');
				$gids[]=$this->goodsattr->where("gavalue='{$attvalue}'")->lists('goods_gid');
				
			}
		}
		//如果点击了“全部”之外的属性
		if($gids){
			//取交集
			$gidsTemp=$gids[0];
			foreach ($gids as $v){
				$gidsTemp=array_intersect($gidsTemp, $v);
				
			}
			//和当前分类对应的gid再次取交集
			return array_intersect($gidsTemp, $cidGids);
		}else{
			return $cidGids;
		}
	}	
	
	//通过gid获得商品的属性
	private function gidGetAttr($gids){
//		p($gids);
		$data=$this->goodsattr->where("goods_gid in(".implode(',',$gids).")")->groupBy('gavalue')->get();
//		p($data);
		//1把相同类型属性id的归结在一起
		$sameAttr=array();
		foreach ($data as $v){
			$sameAttr[$v['t_attr_taid']][]=$v;
		}
//		p($sameAttr);
		//2组合数组
		$temp=array();
		foreach ($sameAttr as $taid => $v) {
			$temp[]=array(
				'name'=>Db::table('t_attr')->where("taid={$taid}")->pluck('attname'),
				'value'=>$v
			);
		}
//		p($temp);
		return $temp;
		
	}
	
	
	
	
	
	//通过cid获得gid
	private function cidGetGids($cid){
		//1通过当前的cid获得对应的所有子集的cid
		$cids=$this->getSonCidds($this->cate->get(),$cid);
		$cids[]=$cid;
		//2通过子集cid查找对应的商品的gid
		$gids=$this->model->where("classify_cid in(".implode(',',$cids).")")->lists('gid');
		return $gids;
	}
	//递归获得指定cid的所有子集
	private function getSonCids($data,$cid){
		$temp=array();
		foreach ($data as $v){
			if($v['pid']==$cid){
				$temp[]=$v['cid'];
				$temp=array_merge($temp,$this->getSonCids($data,$v['cid']));
			}
		}
		return $temp;
	}
	
}
?>	